package com.xie.authority_system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xie.authority_system.dto.RolePermissionDTO;
import com.xie.authority_system.entity.Role;
import com.xie.authority_system.entity.User;
import com.xie.authority_system.service.PermissionService;
import com.xie.authority_system.service.RoleService;
import com.xie.authority_system.utils.Result;
import com.xie.authority_system.vo.RolePermissionVo;
import com.xie.authority_system.vo.query.RoleQueryVo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * @author 谢金成
 * @date 2024/1/8 14:58
 */
@RestController
@RequestMapping("/api/role")
public class RoleController {
    @Resource
    private RoleService roleService;

    @Resource
    private PermissionService permissionService;

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:role:select')")
    public Result list(RoleQueryVo roleQueryVo) {
        IPage<Role> page = new Page<>(roleQueryVo.getPageNo(), roleQueryVo.getPageSize());
        roleService.findRoleListByUserId(page, roleQueryVo);
        return Result.ok(page);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:role:add')")
    public Result add(@RequestBody Role role) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        role.setCreateUser(user.getId());
        if (roleService.save(role)) {
            return Result.ok().message("角色添加成功");
        }

        return Result.error().message("角色添加失败");
    }

    @PutMapping("/update")
    @PreAuthorize("hasAuthority('sys:role:edit')")
    public Result update(@RequestBody Role role) {
        if (roleService.updateById(role)) {
            return Result.ok().message("角色修改成功");
        }

        return Result.error().message("角色修改失败");
    }

    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public Result delete(@PathVariable Long id) {
        if (roleService.deleteRoleById(id)) {
            return Result.ok().message("角色删除成功");
        }

        return Result.error().message("角色删除失败!");
    }

    @GetMapping("/check/{id}")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public Result check(@PathVariable Long id) {
        if (roleService.hasRoleCount(id)) {
            return Result.exist().message("该角色已分配给其他用户使用, 无法删除");
        }

        return Result.ok();
    }

    @GetMapping("/getAssignPermissionTree")
    @PreAuthorize("hasAuthority('sys:role:assign')")
    public Result getAssignPermissionTree(Long userId, Long roleId) {
        RolePermissionVo permissionTree = permissionService.findPermissionTree(userId, roleId);
        return Result.ok(permissionTree);
    }

    @PostMapping("/saveRoleAssign")
    @PreAuthorize("hasAuthority('sys:role:assign')")
    public Result saveRoleAssign(@RequestBody RolePermissionDTO rolePermissionDTO) {
        if (roleService.saveRolePermission(rolePermissionDTO.getRoleId(), rolePermissionDTO.getList())) {
            return Result.ok().message("权限分配成功");
        }

        return Result.error().message("权限分配失败");
    }
}
